package Inji;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class Delete extends JPanel {
	private JLabel lblNewLabel;
	private JTextField username;
	private JButton search;
	private JTextArea textArea;
	private JButton remove;
	
	ArrayList<Data6> list;
	int index;
	boolean check = false;
	private JLabel lblDeleteDate;

	/**
	 * Create the panel.
	 */
	public Delete() {
		setBackground(Color.ORANGE);
		setLayout(null);
		add(getLblNewLabel());
		add(getUsername());
		add(getSearch());
		add(getTextArea());
		add(getRemove());
		add(getLblDeleteDate());

	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("이름 입력");
			lblNewLabel.setForeground(new Color(0, 0, 0));
			lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 11));
			lblNewLabel.setBounds(89, 123, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getUsername() {
		if (username == null) {
			username = new JTextField();
			username.setBounds(166, 120, 149, 21);
			username.setColumns(10);
		}
		return username;
	}
	private JButton getSearch() {
		if (search == null) {
			search = new JButton("검색");
			search.setFont(new Font("굴림", Font.PLAIN, 11));
			search.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					boolean flag = true;
					
					if(!username.getText().equals("")) // 공백이 아니라면
					{
						for(int i=0;i<list.size();i++){
							
							if (username.getText().equals(list.get(i).name)) // 이름 같은걸 찾는다
							{
								flag = false;
								check = true;
								
								index = i; // 인덱스를 얻어온다.
								
								
								SimpleDateFormat sb = new SimpleDateFormat("yy-MM-dd hh:mm");				
								String str = sb.format(list.get(i).nal);
								
								//textArea.append(str+"");
								
								textArea.setText(list.get(i).name +"    "  + list.get(i).address + "    "  + 
										list.get(i).phone +"    "  + list.get(i).email +"    "  + 
										list.get(i).age+"    "  +str);
								break;
							}
						}
					}
					
					if (flag)
						textArea.setText("데이터가 존재하지 않습니다.");
				}
			});
			search.setBounds(360, 119, 97, 23);
		}
		return search;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setFont(new Font("Monospaced", Font.PLAIN, 11));
			textArea.setBounds(89, 157, 368, 24);
		}
		return textArea;
	}
	private JButton getRemove() {
		if (remove == null) {
			remove = new JButton("삭제");
			remove.setFont(new Font("굴림", Font.PLAIN, 11));
			remove.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// 삭제 버튼을 누르면, 해당 행의 데이터를 삭제하고 배열을 정리한다. 

					if (check){
						list.remove(index);
						textArea.setText("삭제되었습니다.");
					}
					else
						textArea.setText("데이터를 입력하지 않았습니다.");
				}
			});
			remove.setBounds(360, 209, 97, 23);
		}
		return remove;
	}
	
	public void setD (ArrayList<Data6> list){
		this.list = list;  
	}
	private JLabel getLblDeleteDate() {
		if (lblDeleteDate == null) {
			lblDeleteDate = new JLabel("            Delete Date");
			lblDeleteDate.setOpaque(true);
			lblDeleteDate.setForeground(Color.WHITE);
			lblDeleteDate.setFont(new Font("Consolas", Font.PLAIN, 17));
			lblDeleteDate.setBackground(Color.DARK_GRAY);
			lblDeleteDate.setBounds(315, 10, 220, 21);
		}
		return lblDeleteDate;
	}
}
